import { createRouter, createWebHistory } from 'vue-router'
// import HomeView from '../views/home/Home.vue'
import Cookies from "js-cookie";
import Header from '../components/Header.vue'
import Layout from "@/views/Layout";
const routes = [
  {
    path: '/login',
    name: 'Login',
    component: () => import("../views/home/Login"),
  },
  {
    path: '/',
    name: 'Layout',
    component:Layout,
    //redirect实现重定向 从Layout定向到home
    redirect:"home",
    children:[
      // { path: '/header', name: 'Header', component: Header},
      { path:'home',name:'Home',component:() =>import("@/views/home/Home")},
      // { path:'/personality', name:'Personality', component:() => import("@/views/home/Personality")},

    //    =====导览=====
      { path:'/visit',name:'Visit',component:() =>import("@/views/visit/Visit")},
    //   { path:'/appointment',name:'Appointment',component:() =>import("@/views/home/Home")},
      { path:'/transport',name:'Transport',component:() => import("@/views/visit/Transport")},
      { path:'/notice',name:'Notice',component:() =>import("@/views/visit/Notice")},


    //    =====展览=====
      {path:'/recentExhibition',name:'recentExhibition',component:() => import("@/views/exhibition/recentExhibition")},
      {path:'/specialMuseum',name:'specialMuseum',component:() => import("@/views/exhibition/specialMuseum")},
      {path:'/appointment',name:'Appointment',component:() => import("@/views/exhibition/Appointment")},
      {path:'/exhibitionDetail',name:'ExhibitionDetail',component:() => import("@/views/exhibition/ExhibitionDetail")},
      // {path:'/localWeather',name:'localWeather',component:() => import("@/components/LocalWeather")},
      {path:'/museumDetail',name:'museumDetail',component:() => import("@/views/exhibition/MusemDetail")},
    //    =====文创商城=====
      {path:'/mall',name:'Mall',component:() => import("@/views/products/Mall")},
      {path:'/productsDetail',name:'productsDetail',component:() => import("@/views/products/ProductsDetail")},

    //    =====个人中心=====
      { path: '/person', name: 'Person', component: ()=> import('@/views/user/Person')},
      { path: '/order', name: 'Order', component: ()=> import('@/views/user/Order')},
      { path: '/museumOrder', name: 'MuseumOrder', component: ()=> import('@/views/user/order/MuseumOrder')},
      { path: '/ue4', name: 'ue4', component: ()=> import('@/views/home/UE4')},
    ]
  },

  { path:'/:pathMatch(.*)', component:() => import('@/views/404')}


]

const router = createRouter({
  history: createWebHistory(process.env.BASE_URL),
  routes
})

//路由守卫,如果当前路径不是login,强制跳转到login
//用户侧是不能采用路由守卫的
router.beforeEach((to,from,next)  => {
  if(to.path === '/login') next()
  const user = Cookies.get("user")
  if(!user && to.path !== '/login')
    return next("/login")
  //访问 /home的时候,且cookie里存放数据,此时直接放行
  next()
})

router.afterEach((to, from, next) => {
  let title = "宁夏数字工艺博物馆";
  document.title = title;
})
export default router
